

/*------------------------------------------------------------------------------
	1. Housekeeping
------------------------------------------------------------------------------*/

	*Run housekeeping code
		qui do ".../replication_package/housekeeping.do"

	*Write log
		log using "$log_loc/table_a3.log", text replace

		clear
		tempfile table_a3
		save `table_a3', emptyok

/*------------------------------------------------------------------------------
	2. Clean Main Datasets
------------------------------------------------------------------------------*/

	forvalues year = 2017/2021 {
		*Import Data
		use "$data/childrens`year'.dta", clear

			/*Restrict Sample*/
				*Keep f1095 filings.
					rename state_insurance state
					fmerge m:1 state using "$data/statelist.dta"
						assert _merge != 2 //Confirm that all 51 states are in dataset
						drop if _merge != 3

				*Keep if age_num is within the range
					keep if age_num >= 0 & age_num <= 16

				*Keep if kid died after end of year or is currently alive
					keep if `year'*10000+1231<death_date|death_date==0

				*Keep if months covered is possible
					keep if num_cov_ins > 0 & num_cov_ins <= 12
	
			/*Generate variables for table*/	
				*Generate a dummy variable for medicaid coverage_num
					gen mcaid=num_cov_ins_5>=1&num_cov_ins_5<=12

				*Generate outcome variable on claiming
					gen onreturn = file_inc != .

				*Generate a ct variable
					gen ct = 1

				*Generate variable for year
					gen year = `year'

/*------------------------------------------------------------------------------
	3. Export Backup Data for Table 2
------------------------------------------------------------------------------*/

			/*Prepare backup data*/
				*Collapse to the by-age_num level		
					collapse (sum) ct onreturn, by(year mcaid)
					
				*Adjust for Claiming Limits on electronic filing
					replace onreturn = onreturn * 1.0017029

				*Reshape wide such that there are separate values for ct and onreturn
					reshape wide ct onreturn, i(year) j(mcaid)
		
				*Create percent claimed variable
					gen perc_claimed = (onreturn0 + onreturn1)/(ct0 + ct1)	
					gen perc_claimed_mcaid = onreturn1/ct1
					gen perc_claimed_no_mcaid = onreturn0/ct0

				*Keep key outcomes
					keep perc_claimed perc_claimed_mcaid perc_claimed_no_mcaid year

		*Save results as a tempfile
			tempfile results_`year'
			save `results_`year'', replace
	}

	*Import and append the results from 2017 to 2021
		forvalues year = 2017/2021 {
			use `results_`year'', clear
		
			append using `table_a3'
			tempfile table_a3
			save `table_a3', replace
		}

		*Organize before export
			sort year 
			order year perc_claimed perc_claimed_mcaid perc_claimed_no_mcaid

	*Export Results as an Excel File
		export excel using "$output/table_backup_data.xlsx", first(var) sheet("claim_by_medicaid", replace)